home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-2 / Inter.Net 55-2.iso / Mandrake / mdkinst / usr / bin / perl-install / standalone / adduserdrake next >
Encoding:
Text File  |  2000-01-12  |  2.7 KB  |  92 lines

  1. #!/usr/bin/perl
  2.  
  3. use lib qw(/usr/lib/libDrakX);
  4.  
  5. use common qw(:common :functional :system :file);
  6. use interactive;
  7. use any;
  8.  
  9. local $_ = join '', @ARGV;
  10.  
  11. /-h/ and die "usage: adduserdrake [--beginner] [--expert] [<users...>]\n";
  12.  
  13. $::beginner = /-beginner/;
  14. $::expert = /-expert/;
  15. $::isStandalone = 1;
  16.  
  17. my @etc_pass_fields = qw(name pw uid gid realname home shell);
  18. my @shells = grep { -x $_ } map { "/bin/$_" } qw(bash tcsh zsh ash ksh);
  19. my $isMD5 = cat_("/etc/pam.d/passwd") =~ /md5/;
  20. my $isShadow = cat_("/etc/pam.d/passwd") =~ /shadow/;
  21. my $security = $ENV{SECURE_LEVEL};
  22.  
  23.  
  24.  
  25. if (my @l = grep { ! /^-/ } @ARGV) {
  26.     addusers(map {{ name => $_, realname => $_ }} @l);
  27.     exit 0;
  28. }
  29.  
  30. my $in = vnew interactive('su');
  31.  
  32. new:
  33. if ($in->ask_from_entries_refH(
  34.         [ _("Add user"), _("Accept user"), _("Done") ],
  35.         _("Enter a user\n%s", $users ? _("(already added %s)", join(", ", map { $_->{realname} || $_->{name} } @users)) : ''),
  36.         [ 
  37.      _("Real name") => \$u->{realname},
  38.      _("User name") => \$u->{name},
  39.        $security < 2 ? () : (
  40.          _("Password") => {val => \$u->{password}, hidden => 1},
  41.          _("Password (again)") => {val => \$u->{password2}, hidden => 1},
  42.        ), $::beginner ? () : (
  43.          _("Shell") => {val => \$u->{shell}, list => \@shells, not_edit => !$::expert} 
  44.        ),
  45.         ],
  46.         focus_out => sub {
  47.         if ($_[0] eq 0) {
  48.         $u->{name} ||= lc first($u->{realname} =~ /((\w|-)+)/);
  49.         }
  50.     },
  51.         complete => sub {
  52.         $u->{password} eq $u->{password2} or $o->ask_warn('', [ _("The passwords do not match"), _("Please try again") ]), return (1,3);
  53.         $security > 3 && length($u->{password}) < 6 and $o->ask_warn('', _("This password is too simple")), return (1,2);
  54.         $u->{name} or $o->ask_warn('', _("Please give a user name")), return (1,0);
  55.         $u->{name} =~ /^[a-z0-9_-]+$/ or $o->ask_warn('', _("The user name must contain only lower cased letters, numbers, `-' and `_'")), return (1,0);
  56.         member($u->{name}, map { $_->{name} } @users) and $o->ask_warn('', _("This user name is already added")), return (1,0);
  57.         return 0;
  58.     },
  59. )) {
  60.     push @users, $u;
  61.     $u = {};
  62.     goto new;
  63. }
  64.  
  65. addusers(@users);
  66.  
  67. sub addusers {
  68.     my @u = map { $_->{name} } my @users = @_;
  69.  
  70.     foreach (@users) {
  71.     $_->{pw} = $isMD5 ? c::crypt_md5($_->{password}, salt(8)) : crypt($_->{password}, salt(2));
  72.     $_->{shell} ||= "/bin/bash";
  73.     }
  74.  
  75.     system("adduser $_") foreach @u;
  76.     any::addUsers('', @u);
  77.  
  78.     substInFile {
  79.     foreach my $u (@users) {
  80.         if (/^$u->{name}:/) {
  81.         chomp;
  82.         my %l; @l{@etc_pass_fields} = split ':';
  83.         add2hash($u, \%l);
  84.         $_ = join(':', @$u{@etc_pass_fields}) . "\n";
  85.         }
  86.     }
  87.     } "/etc/passwd";
  88.     system("pwconv") if $isShadow;
  89. }
  90.  
  91. $in->exit(0);
  92.